//
//  HRDynamicInfoTableViewModel.m
//  Hear
//
//  Created by mac on 2017/4/11.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRDynamicInfoTableViewModel.h"
#import "HRDynamicCommentTableViewCell.h"
#import "HRDynamicCommentModel.h"
#import "HRDynamicViewController.h"

@interface HRDynamicInfoTableViewModel ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)HRDynamicCommentModel *lastCommentModel;
@property (nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)HRDynamicCommentTableViewCell *tempCell;

@end
@implementation HRDynamicInfoTableViewModel
-(NSMutableArray *)contentArray{
    if (_contentArray == nil){
        _contentArray = [NSMutableArray array];
    }
    return _contentArray;
}


- (void)handleWithTable:(UITableView *)table
{
    self.tableView = table;
    table.delegate = self;
    table.dataSource = self;

}




#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.contentArray.count;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    HRDynamicCommentModel *model = self.contentArray[indexPath.row];
    return model.cellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    WeakSelf
    HRDynamicCommentTableViewCell *cell = [HRDynamicCommentTableViewCell cellWithTableView:tableView];
    
    HRDynamicCommentModel *model = self.contentArray[indexPath.row];

    
    cell.iconTapBock = ^(NSString *currentUserID){
        if (weakSelf.iconTapBock){
            weakSelf.iconTapBock(model.UserLocalId);
        }
    };
    if (model)
    {
        cell.commentModel = model;
    }
    cell.playCommentMedioBlock = ^(HRDynamicCommentModel *currentCommentModel){
        if (currentCommentModel || weakSelf.lastCommentModel){
            
            dispatch_async(dispatch_get_main_queue(), ^{
                if(![currentCommentModel.LocalId isEqualToString:weakSelf.lastCommentModel.LocalId]){
                    weakSelf.lastCommentModel.isPlaying = NO;
                    [weakSelf.tempCell.soundButton.voiceImageView stopAnimating];
                }
                if (model.isPlaying){
                    [cell.soundButton.voiceImageView startAnimating];
                    
                }else{
                    [cell.soundButton.voiceImageView stopAnimating];
                }
                weakSelf.lastCommentModel = currentCommentModel;
                weakSelf.tempCell = cell;
            });
            
        }
    
        
    };
    
    cell.commentClickBlock = ^{
        if (weakSelf.commentClickBlock){
            weakSelf.commentClickBlock(model);
        }
    };
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    HRDynamicCommentModel *model = self.contentArray[indexPath.row];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.clickIndexrowBlock){
        self.clickIndexrowBlock(model);
    }
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    if (!self.contentArray.count) return NO;
    HRDynamicCommentModel *model = self.contentArray[indexPath.row];
    if (model){
        if ([model.UserLocalId isEqualToString:USERID]){
            return YES;
        }
    }
    return NO;
}



- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        if (self.deleteCommentBlock){
            self.deleteCommentBlock(indexPath.row);
        }
    }
    
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if (self.scrollViewWillBeginDraggingBlock){
        self.scrollViewWillBeginDraggingBlock();
    }
}




@end
